/**
 * 
 */
package cn.crm.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.InOutLog;
import cn.crm.bean.Product;
import cn.crm.dao.InOutLogDao;

/**
 * @author wbh
 * 
 */
@Repository("InOutLogDao")
public class InOutLogDaoImpl extends HibernateDaoSupport implements InOutLogDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.InOutLogDao#save(cn.crm.bean.InOutLog)
	 */
	@Override
	public String save(InOutLog log) {
		// TODO Auto-generated method stub
		return (String) super.getHibernateTemplate().save(log);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.InOutLogDao#del(cn.crm.bean.InOutLog)
	 */
	@Override
	public String del(InOutLog log) {
		String result = "";
		super.getHibernateTemplate().update(log);
		result = "success";
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.InOutLogDao#upd(cn.crm.bean.InOutLog)
	 */
	@Override
	public String upd(InOutLog log) {
		String result = "";
		super.getHibernateTemplate().update(log);
		result = "success";
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.InOutLogDao#show(cn.crm.bean.InOutLog)
	 */
	@Override
	public List<InOutLog> show(Integer firstResult, Integer pageSize,
			Object... params) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(InOutLog.class);
		criteria.add(Restrictions.eq("iolIsdelete", false));
		if (params != null) {
			for (Object object : params) {
				if (object != null) {
					criteria.add((Criterion) object);
				}
			}
		}
		List<InOutLog> list = super.getHibernateTemplate().findByCriteria(
				criteria, firstResult, pageSize);
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.dao.InOutLogDao#get(java.lang.String)
	 */
	@Override
	public InOutLog get(String id) {
		// TODO Auto-generated method stub
		return super.getHibernateTemplate().get(InOutLog.class, id);
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		Session session = super.getSession();
		Object count;
		Query query = session
				.createQuery("select count(iolPsid) from InOutLog  where iolIsdelete=false");
		count = query.uniqueResult();
		return Integer.parseInt(count.toString());
	}

	@Override
	public int getcount(Object... params) {
		// TODO Auto-generated method stub
		Session session =super.getSession();
		
		Criteria cri=session.createCriteria(InOutLog.class);
		cri.setProjection(Projections.rowCount());
		if (params!=null) {
			for (Object res : params) {
				if (res!=null) {
					System.out.println(res);
					cri.add((Criterion)res);
				}
			}
		}
		List list=cri.list();
		return Integer.parseInt((list.size()>0?list.get(0):0).toString());
	}

}
